package com.example.address.service.impl;


import com.example.address.bean.Address;
import com.example.address.bean.ResultVO;
import com.example.address.mapper.AddressMapper;
import com.example.address.service.IAddressService;
import com.example.address.service.IDistrictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.file.AccessDeniedException;
import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements IAddressService {
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private IDistrictService districtService;

    @Override
    public ResultVO addNewAddress(Address address) {

        // 补全数据：省、市、区的名称
        String provinceName = districtService.getNameByCode(address.getProvinceCode());
        String cityName = districtService.getNameByCode(address.getCityCode());
        String areaName = districtService.getNameByCode(address.getAreaCode());
        address.setProvinceName(provinceName);
        address.setCityName(cityName);
        address.setAreaName(areaName);

//        // 补全数据：根据以上统计的数量，得到正确的isDefault值(是否默认：0-不默认，1-默认)，并封装
//        Integer isDefault = count == 0 ? 1 : 0;
        address.setIsDefault(0);

        // 补全数据：4项日志
        Date now = new Date();
        address.setCreatedUser(address.getName());
        address.setCreatedTime(now);
        address.setModifiedUser(address.getName());
        address.setModifiedTime(now);

        // 调用addressMapper的insert(Address address)方法插入收货地址数据，并获取返回的受影响行数
        Integer rows = addressMapper.insert(address);
        // 判断受影响行数是否不为1
        if (rows > 0) {
            return new ResultVO(1, "添加成功！", address);
        } else {
            return new ResultVO(0, "添加失败！", null);
        }
    }


    @Transactional
    @Override
    public ResultVO delete(Integer aid) {

        // 根据参数aid，调用deleteByAid()执行删除
        Integer rows = addressMapper.deleteByAid(aid);
        if (rows > 0) {
            return new ResultVO(1, "删除成功！", null);
        } else {
            return new ResultVO(0, "删除失败！", null);
        }
    }

    @Override
    public ResultVO listAddrs() {
        // 根据收货地址数据id，查询收货地址详情
        List<Address> address = addressMapper.listAddrs();

        if (address.size() > 0) {
            return new ResultVO(1, "查询成功！", address);
        } else {
            return new ResultVO(0, "查询失败！", null);
        }
    }

    @Override
    public ResultVO getByAid(Integer aid) {
        // 根据收货地址数据id，查询收货地址详情
        Address address = addressMapper.findByAid(aid);

        if (address == null) {
            return new ResultVO(0, "查询失败！", null);
        } else {
            return new ResultVO(1, "查询成功！", address);
        }
    }

    @Override
    public ResultVO updateByAid(Address address) {
        // 补全数据：省、市、区的名称
        String provinceName = districtService.getNameByCode(address.getProvinceCode());
        String cityName = districtService.getNameByCode(address.getCityCode());
        String areaName = districtService.getNameByCode(address.getAreaCode());
        address.setProvinceName(provinceName);
        address.setCityName(cityName);
        address.setAreaName(areaName);

//        // 补全数据：根据以上统计的数量，得到正确的isDefault值(是否默认：0-不默认，1-默认)，并封装
//        Integer isDefault = count == 0 ? 1 : 0;
        address.setIsDefault(0);

        // 补全数据：2项日志
        Date now = new Date();
        address.setModifiedUser(address.getName());
        address.setModifiedTime(now);

        // 调用addressMapper的insert(Address address)方法插入收货地址数据，并获取返回的受影响行数
        Integer rows = addressMapper.updateByAid(address);
        // 判断受影响行数是否不为1
        if (rows > 0) {
            return new ResultVO(1, "修改成功！", address);
        } else {
            return new ResultVO(0, "修改失败！", null);
        }
    }
}
